<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head th:include="include :: header"></head>
<style>
    .fixed-table-container tbody .selected td{background-color:#0073B7!important;color:#ffffff!important;}
</style>
<body class="hold-transition skin-blue sidebar-mini">
<section class="content-header"><h1>采购计划审阅列表</h1></section>
<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-header">
                    <form id="queryForm" onsubmit="return false;">
                        <div class="row">
                            <div class="form-inline">
                                <div class="form-group">
                                    <label>物料描述</label>
                                    <input type="text" name="sppMatDesc" class="form-control" placeholder="请输入提报或采购物料">
                                </div>
                                <div class="form-group">
                                    <label>SAP物料编码</label>
                                    <input type="text" name="sppMatCode" class="form-control" placeholder="请输入SAP物料编码">
                                </div>
                                <div class="form-group">
                                    <label>项目编号</label>
                                    <input type="text" name="sppProjectCode" class="form-control" placeholder="请输入项目编号">
                                </div>
                                <div class="form-group">
                                    <label>项目名称</label>
                                    <input type="text" name="sppProjectName" class="form-control" placeholder="请输入项目名称">
                                </div>
                                <div class="form-group">
                                    <label class="control-label">库位</label>
                                    <select class="form-control" name="sppWhiId" title="">
                                        <option value="">全部</option>
                                        <option th:each="stock : ${stocks}" th:value="${stock.whiId}"
                                                th:text="${stock.whiName}"></option>
                                    </select>
                                </div>
                                <div style="display: none" class="form-group">
                                    <input name="sppPlanner" style="display: none" type="hidden">
                                </div>
                                <div class="form-group">
                                    <label>审阅状态</label>
                                    <select name="sppReviewFlag" class="form-control">
                                        <option value="0" selected="selected">未阅</option>
                                        <option value="1">已阅</option>
                                    </select>
                                </div>
                                <div class="form-group">
                                    <label>提报日期</label>
                                    <input type="text" class="form-control datepicker" name="startTime"
                                           readonly="readonly"/>
                                    <span> 至 </span>
                                    <input type="text" class="form-control datepicker" name="endTime"
                                           readonly="readonly"/>
                                </div>
                                <button type="button" class="btn btn-success" id="btn-search">
                                    <span class="Bold">搜索</span>
                                </button>

                            </div>
                        </div>
                    </form>
                </div>
                <div class="box-body">
                    <div id="toolbar" class="form-inline">
                        <a id="btn_notRead" shiro:hasPermission="buy:buycheck:edit" href="javascript:void(0);"
                           class="btn btn-danger pull-right" style="margin-right: 10px" onclick="cancelAll()">
                            <span class="Bold">批量未阅</span>
                        </a>
                        <a  id="btn_read" shiro:hasPermission="buy:buycheck:edit" href="javascript:void(0);"
                           class="btn btn-success pull-right" style="margin-right: 10px" onclick="commitAll()">
                            <span class="Bold">批量已阅</span>
                        </a>
                    </div>
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
    </div>
</section>
<div th:include="include :: footer"></div>
<script th:inline="javascript">
    $(function () {
        var title = [[${title}]];
        $('title').html( $('title').html()+' - '+title);
    })
    var table;
    var statusName = ["未审阅", "已审阅"];
    var colors = ['label-default', 'label-success'];
    var selectStatus = 1;
    /** 审批采购计划 */
    function commit(id) {
        js.confirmPost({
            content: "确认已阅吗？",
            url: ctx + "buy/buyPlanCheck/commit",
            data: {id: id},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }

    /** 未阅采购计划 */
    function cancel(id) {
        js.confirmPost({
            content: "确认未阅吗？",
            url: ctx + "buy/buyPlanCheck/cancel",
            data: {id: id},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        });
    }

    /** 批量已阅采购计划 */
    function commitAll() {
        var arr = table.bootstrapTable('getAllSelections');
        var selectArrSppIds=new Array()
        for (var i = 0; i < arr.length; i++) {
           if($.inArray(arr[i].rppSppId,selectArrSppIds) < 0){
                selectArrSppIds.push(arr[i].rppSppId);
            }
        }

        // var arr = js.table.selectColumns(table, "sppId");
        if (selectArrSppIds.length < 1) {
            js.modal.warning("请选择要已阅的计划");
            return;
        }
        js.confirmPost({
            content: "确认全部已阅吗？",
            url: ctx + "buy/buyPlanCheck/commitAll",
            data: {ids: selectArrSppIds},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.table.refresh(table);
                    js.modal.success(result.msg);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }

    /** 批量未阅采购计划 */
    function cancelAll() {
        var arr = table.bootstrapTable('getAllSelections');
        var selectArrSppIds=new Array()
        for (var i = 0; i < arr.length; i++) {
            if($.inArray(arr[i].rppSppId,selectArrSppIds) < 0){
                selectArrSppIds.push(arr[i].rppSppId);
            }
        }
        // var arr = js.table.selectColumns(table, "sppId");
        if (selectArrSppIds.length < 1) {
            js.modal.warning("请选择要未阅的计划");
            return;
        }
        js.confirmPost({
            content: "确认全部未阅吗？",
            url: ctx + "buy/buyPlanCheck/cancelAll",
            data: {ids: selectArrSppIds},
            success: function (result) {
                if (result.type === web_status.SUCCESS) {
                    js.modal.success(result.msg);
                    js.table.refresh(table);
                } else {
                    js.modal.warning(result.msg);
                }
            }
        })
    }

    $(function () {
        $("title").html($("title").html()+" —— 采购计划审阅");
        $("#btn_notRead").hide();
        $("#queryForm [name='sppReviewFlag']").change(function () {
            if($(this).val()==0){
                selectStatus=1;
                $("#btn_notRead").hide();
                $("#btn_read").show();

            }else {
                selectStatus=2;
                $("#btn_notRead").show();
                $("#btn_read").hide();

            }
            js.table.refresh(table);
        });
        /** 加载用户表格 */
        table = js.table.init({
            url: ctx + "buy/buyPlanCheck/list",
            showExport: false,
            showColumns: false,
            showRefresh: true,
            clickToSelect: true,
            onLoadSuccess: function (data) {
                // js.table.merge(table,
                //     data,
                //     "rppSppId",
                //     "sppId",
                //     "sppMatDesc",
                //     "sppSapCode",
                //     "sppBuyer",
                //     "sppPayCycle",
                //     "sppNum",
                //     "whiName",
                //     "sppProjectName",
                //     "sppProjectCode",
                //     "sppStatus",
                //     "rppPlanner",
                //     "op");
            },
            columns: [
                {
                    checkbox: true,
                    width: '30px',
                    field: 'sppId',
                    formatter: function (value, row, index) {
                        if(selectStatus==1){
                            if (row.sppReviewFlag === 0) {
                                return {disabled: false}
                            } else {
                                return {disabled: true}
                            }
                        }

                    }
                },
                {
                    title: '序号', width: '50px',
                    formatter: function (value, row, index, field) {
                        return index + 1;
                    }
                },
                {
                    title: "提报物料描述", field: 'rppMatDesc',
                    formatter: function (value, row, index, field) {
                        return value;
                    }
                },
                {
                    title: "采购物料描述", field: 'sppMatDesc',
                    formatter: function (value, row, index, field) {
                        return value;
                    }
                },
                {title: "SAP物料编码", field: 'sppSapCode',width:'120'},
                {title: "业务员", field: 'sppBuyer',width:'70'},
                {title: "交货周期", field: 'sppPayCycle',width:'70'},
                {title: "采购数量", field: 'sppNum',width:'70'},
                {title: "库位", field: 'whiName',width:'70'},
                {
                    title: "备注计划用途", field: 'sppProjectName',
                    formatter: function (value, row, index, field) {
                        return value;
                    }
                },
                {title: "使用周期", field: 'sppProjectCode',width:'90'},
                {
                    title: "审阅状态", field: 'sppReviewFlag',width:'90',
                    formatter: function (value, row, index, field) {
                        return '<span class="badge ' + colors[value] + '">' + statusName[value] + '</span> ';
                    }
                },
                {title: "计划员", field: 'rppPlanner',width:'70'},
                {
                    title: "操作",
                    field: 'op',
                    width: '110px',
                    formatter: function (value, row) {
                        var actions = [];
                        if (hasP("buy:buycheck:edit")) {
                            if (row.sppReviewFlag === 0) {
                                actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="commit(' + row.rppSppId + ')"> 已阅</a> ');

                            }else {
                                actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="cancel(' + row.rppSppId + ')"> 未阅</a> ');
                            }
                        }
                        return actions.join('');
                    }
                }
            ]
        });

        /** 表格搜索 */
        $("#btn-search").click(function () {
            js.table.search(table);
        });


    });

</script>
</body>
</html>
